<script setup>
import {reactive} from "vue";
import {requestAllWares, requestGetInfo} from "@/api";

const storageGroup = reactive({
  userinfo: uni.getStorageSync("userInfo"),
  initUserInfo() {
    if (this.userinfo.login_state) {
      collectGroup.getCollectList(this.userinfo.account);
    }
  }
})

const routerGroup = reactive({
  linkItemInfo(ware) {
    uni.navigateTo({url: "../itemInfo/itemInfo?ware=" + JSON.stringify(ware)});
  }
})

const collectGroup = reactive({
  baseUrl: "http://118.178.254.102:1253/",
  collectList: [],
  async getCollectList(account) {
    let result = await requestGetInfo({account: account});
    if (result.statusCode === 200) {
      this.collectList = result.data[0].collect.split('_');
      await waresGroup.getWares(this.collectList);
    }
  }
})

const waresGroup = reactive({
  currentWares: [],
  wares: [],
  async getWares(collectList) {
    this.wares = [];
    let result = await requestAllWares();
    if (result.statusCode === 200) {
      this.wares = result.data;
      this.wares.forEach((item)=>{
        collectList.forEach((name)=>{
          if (item.name === name){
            this.currentWares.push(item);
          }
        })
      })
    }
  },
})

storageGroup.initUserInfo();
</script>

<template>
  <view class="collect">
    <scroll-view scroll-y="true">
      <view class="container-box">
        <view class="main">
          <view v-for="(item,index) of waresGroup.currentWares" :key="index"
                @click="routerGroup.linkItemInfo(item)">
            <van-card
                :price="100"
                :tag="item.type"
                :thumb="collectGroup.baseUrl+item.img.split('_')[0]"
                :title="item.name"
                desc="已收藏"
            >
            </van-card>
          </view>
        </view>
      </view>
      <view class="message"></view>
    </scroll-view>
  </view>
</template>

<style scoped src="../../css/collect.css">

</style>